require('../less/sportIng.less')
require('../fonts/iconfont.css')
const utils = require('../lib/commonJs/utils')
const {
    $http
} = require('../lib/commonJs/http.js')
/**
 * 获取元素
 */
const appSportIng = document.querySelector('.app-sportIng')
const appCountdown = document.querySelector('.app-countdown')
const appMap = document.querySelector('.app-map')

const timerText = document.querySelector('.timerText')
const mapTimerText = document.querySelector('.mapTimerText')
const distanceText = document.querySelector('.distanceText')
const sudu = document.querySelector('.sudu')
const mapSudu = document.querySelector('.mapSudu')
const calorieText = document.querySelector('.calorieText')
const mapCalorieText = document.querySelector('.mapCalorieText')

const suspendBtn = document.querySelector('.suspend')
const continueBtn = document.querySelector('.continue')
const stopBtn = document.querySelector('.stop')
const iconMap2 = document.querySelector('.icon-map2')

const mapBack = document.querySelector('.mapBack')
const mapCompletion = document.querySelector('.mapCompletion')


// 是否登录
utils.isLogin()

// 获取初始时间
const time = document.querySelector('.time')

// 获取初始化时间
let normalTimer = time.textContent

// 实例化地图
let map = new BMap.Map('map');

// 点数
let count = 0;

// 定时器
let IntervaltimerText = null;
let sportPosition = [];

let timer = setInterval(function () {
    normalTimer--;
    if (normalTimer > 0) {
        time.textContent = normalTimer
    } else if (normalTimer == 0) {
        time.textContent = 'GO'
        clearInterval(timer)
        setTimeout(() => {
            appSportIng.style.display = 'block'
            appCountdown.style.display = 'none'
            setTimerText()
        }, 1000)
    }
}, 1000)


// 设置初始时间
let sportTimer = 0;

function setTimerText() {
    IntervaltimerText = setInterval(() => {
        sportTimer++
        setMap()
        timerText.textContent = utils.getTimer(sportTimer);
        mapTimerText.textContent = utils.getTimer(sportTimer);
    }, 1000)
}


// 暂停按钮
suspendBtn.addEventListener('click', function () {
    this.style.display = 'none'
    continueBtn.style.display = 'flex'
    stopBtn.style.display = 'flex'
    clearInterval(IntervaltimerText)
})

// 继续按钮
continueBtn.addEventListener('click', function () {
    this.style.display = 'none'
    stopBtn.style.display = 'none'
    suspendBtn.style.display = 'flex'
    setTimerText()
})

// 结束按钮
stopBtn.addEventListener('click', function () {
    appSportIng.style.display = 'none'
    appMap.style.display = 'block'
    mapCompletion.style.display = 'block'
    utils.addMarker(map, sportPosition[count - 1], require('../imgs/end.png'))
})

// 打开地图
iconMap2.addEventListener('click', function () {
    appSportIng.style.display = 'none'
    appMap.style.display = 'block'
    mapBack.style.display = 'block'
})

// 地图返回
mapBack.addEventListener('click', function () {
    this.style.display = 'none'
    appSportIng.style.display = 'block'
    appMap.style.display = 'none'
})

// 设置地图
let kilometre_box = 0
let sportLong = 0;
let kale = 0;

function setMap() {
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            onComplete(r)
        } else {
            console('failed' + this.getStatus());
        }
    });

    //解析定位结果
    function onComplete(data) {
        // 前进长度
        sportLong += 0.0001
        let lng = data.point.lng + sportLong
        let lat = data.point.lat + sportLong
        let point = new BMap.Point(lng, lat);
        // 设置地图中心点
        map.centerAndZoom(point, 15);
        // 保存两点
        sportPosition.push({
            lng,
            lat
        })
        count++
        for (let i = 0; i < sportPosition.length - 1; i++) {
            kilometre_box += parseFloat(utils.calcDistance(sportPosition[i], sportPosition[i + 1]))
            utils.addpolyline(map, sportPosition[i], sportPosition[i + 1])
        }
        // 画图
        // 画点
        if (count === 1) utils.addMarker(map, sportPosition[0], require('../imgs/start.png'))
        // 画线

        // 渲染数据
        distanceText.textContent = kilometre_box.toFixed(2)
        sudu.textContent = utils.calcPace(kilometre_box, sportTimer)
        mapSudu.textContent = utils.calcPace(kilometre_box, sportTimer)
        calorieText.textContent = utils.calCalorie(kilometre_box);
        mapCalorieText.textContent = kale = utils.calCalorie(kilometre_box);



    }
}

/**
 * 保存运动数据
 */
mapCompletion.addEventListener('click', function () {

    //配置参数
    const params = {
        id: localStorage.getItem('userId'), //用户id
        type: 1, //跑步
        takeTime: +kilometre_box.toFixed(2), //运动距离
        miles: +(kilometre_box / sportTimer).toFixed(2), //平均速度 - 配速
        averagerate: sportTimer, //运动时间
        calorie: +kale, //卡路里
    }


    //发请求
    $http.post('/sports/save', params, function (res) {
        if (res.status === 0) {
            location.href = './run.html'
        }
    })

})